package com.live555.utils;

import android.os.Environment;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;

/**
 * 功能描述：写文件到SD卡
 *
 * @author：ZC 创建时间：2018/6/26 14:02
 * 最后修改日期：
 */
public class FileTools {

    public final static String getVideoDir() {
        String lastPath = "";
        if (isExistSDCard()) {
            lastPath = getExternalSdPath() + "/Outsourcing/Video/";
            File file = new File(lastPath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } else {
            lastPath = getInternalSdPath() + "/Outsourcing/Video/";
            File file = new File(lastPath);
            if (!file.exists()) {
                file.mkdirs();
            }
        }//end else
        return lastPath;
    }

    public final static String getImageDir() {
        String lastPath = "";
        if (isExistSDCard()) {
            lastPath = getExternalSdPath() + "/Outsourcing/Image/";
            File file = new File(lastPath);
            if (!file.exists()) {
                file.mkdirs();
            }
        } else {
            lastPath = getInternalSdPath() + "/Outsourcing/Image/";
            File file = new File(lastPath);
            if (!file.exists()) {
                file.mkdirs();
            }
        }//end else
        return lastPath;
    }

    /**
     * 判断是否存在sd卡
     */
    public static boolean isExistSDCard() {
        return Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED);
    }

    /**
     * 获得 SD卡路径
     *
     * @return File
     */
    public static File getExternalSdPath() {
        return Environment.getExternalStorageDirectory();
    }

    /**
     * 获取内置Sd卡路径
     *
     * @return File
     */
    public static File getInternalSdPath() {
        return Environment.getDataDirectory();
    }


    public static void writeFileToSDCard(byte[] buffer, String fileAbsolutePath) {
        if (buffer != null) {
            File file = new File(fileAbsolutePath);
            RandomAccessFile raf = null;

            try {
                raf = new RandomAccessFile(file, "rw");
                raf.seek(file.length());
                raf.write(buffer);
            } catch (IOException var13) {
                var13.printStackTrace();
            } finally {
                try {
                    if (raf != null) {
                        raf.close();
                    }
                } catch (IOException var12) {
                    var12.printStackTrace();
                }

            }
        }
    }
}
